/*
   Copyright (c) 2023 李伟国
   shiro-learner is licensed under Mulan PSL v2.
   You can use this software according to the terms and conditions of the Mulan PSL v2. 
   You may obtain a copy of Mulan PSL v2 at:
            http://license.coscl.org.cn/MulanPSL2 
   THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.  
   See the Mulan PSL v2 for more details. 
*/

package cn.learner.controller;

import cn.learner.config.response.ErrorResponseData;
import cn.learner.config.response.ResponseData;
import cn.learner.config.response.SuccessResponseData;
import cn.learner.constant.enumeration.StatusConstantEnum;
import cn.learner.dto.PermissionChildren;
import cn.learner.dto.RolePermissionDto;
import cn.learner.entity.Permission;
import cn.learner.entity.User;
import cn.learner.service.IPermissionService;
import cn.learner.service.IRolePermissionService;
import cn.learner.service.IUserService;
import cn.learner.utils.JWTUtil;
import cn.learner.utils.ObjectCast;
import com.alibaba.druid.support.json.JSONUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping(value = "permission")
public class PermissionController {
    private static final String headerToken = "Authorization";
    @Autowired
    private IUserService userService;

    @Autowired
    private IRolePermissionService rolePermissionService;

    @Autowired
    private IPermissionService permissionService;

    @RequiresPermissions(value = {"permission:tree:get"})
    @RequestMapping(value = "/tree",method = RequestMethod.GET)
    public ResponseData getPermissionTree(){
        return new SuccessResponseData(this.permissionService.getPermissionTree());
    }

    @RequiresPermissions(value = {"permission:get"})
    @RequestMapping(value = "{permissionId}", method = RequestMethod.GET)
    public ResponseData getPermission(@PathVariable("permissionId") Long permissionId){
        return new SuccessResponseData(this.permissionService.getById(permissionId));
    }

    @RequiresPermissions(value = {"permission:insert"})
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResponseData postPermission(@RequestBody Permission permission){
        if(permission == null){
            return new ErrorResponseData();
        }
        permission.setStatus(StatusConstantEnum.ENABLE.getCode());
        return new SuccessResponseData(this.permissionService.save(permission));
    }

    @RequiresPermissions(value = {"permission:update"})
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public ResponseData putPermission(@RequestBody Permission permission){
        return new SuccessResponseData(this.permissionService.updateById(permission));
    }

    @RequiresPermissions(value = {"permission:delete"})
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public ResponseData deletePermission(@RequestBody Permission permission){
        return new SuccessResponseData(this.permissionService.delete(permission));
    }

    @RequiresPermissions(value = {"permission:reset"})
    @RequestMapping(value = "/reset", method = RequestMethod.DELETE)
    public ResponseData resetPermission(@RequestBody Permission permission){
        return new SuccessResponseData(this.permissionService.reset(permission));
    }

    @RequestMapping(value = "/role/{roleId}", method = RequestMethod.GET)
    public ResponseData getByRoleId(@PathVariable("roleId") Long roleId){
        return new SuccessResponseData(this.rolePermissionService.findPermissionIdByRoleId(roleId));
    }
    @RequestMapping(value = "/role", method = RequestMethod.POST)
    public ResponseData postRolePermission(@RequestBody RolePermissionDto rolePermissionDto){
        List<Long> parse  = ObjectCast.castListToLong(JSONUtils.parse(rolePermissionDto.getPermissionIdJson()));
        System.out.println("添加  "+parse.toString());
        rolePermissionService.insertRolePermission(rolePermissionDto.getRoleId(), parse);
        return new SuccessResponseData();
    }

    @RequestMapping(value = "/role", method = RequestMethod.DELETE)
    public ResponseData deleteRolePermission(@RequestBody RolePermissionDto rolePermissionDto){
        List<Long> parse  = ObjectCast.castListToLong(JSONUtils.parse(rolePermissionDto.getPermissionIdJson()));
        System.out.println("删除  "+parse.toString());
        rolePermissionService.deleteRolePermission(rolePermissionDto.getRoleId(), parse);
        return new SuccessResponseData();
    }

    @RequiresAuthentication
    @RequestMapping(value = "/menu", method = RequestMethod.GET)
    public ResponseData getUserMenu(HttpServletRequest request){
        String jwt = request.getHeader(headerToken);
        String username = JWTUtil.getUsername(jwt);
        User user = userService.findByUsername(username);
        return new SuccessResponseData(permissionService.getUserMenuByUserId(user.getId()));
    }
}
